import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class MainForm {

	private static final long serialVersionUID = 3493635987367217622L;

	public static void main(String[] args) throws Exception {

		//config aus DB Laden
		//Class.forName("org.sqlite.JDBC");
		//Connection conn = DriverManager.getConnection("jdbc:sqlite:game.db");
		//Statement stat = conn.createStatement();
		//ResultSet res = stat.executeQuery("SELECT * FROM tabConfig");

		// new main();
	    final JFrame cnt = new JFrame("Game ?!");
		
		//Fenster Zentrieren
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) (dim.getWidth() - 550);
		int y = (int) (dim.getHeight() - 400);
		x = x / 2;
		y = y / 2;
		cnt.setLocation(x,y);
		cnt.setSize(550, 400);
		cnt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                String nick;

                do
                {
                nick = JOptionPane.showInputDialog("Bitte geben Sie ihren Nick an:");
                if(nick.isEmpty()) JOptionPane.showMessageDialog(cnt, "Sie haben vergessen ihren Namen einzugeben !");
                }while(nick.isEmpty());

        final owncanvas game = new owncanvas(nick);
        
		cnt.setResizable(true);
		cnt.add(game);
		cnt.setVisible(true);

        cnt.addComponentListener(new ComponentAdapter()
        {
        @Override
        public void componentResized(ComponentEvent evt) {
        game.width = cnt.getWidth();
        game.height = cnt.getHeight();
        }
        
        }
        );
        
        /**
         * KeyListener um die Tastaturanschläge abzufangen
         */
    cnt.addKeyListener(new KeyListener() {

    public void keyReleased(KeyEvent event)
    {
        int auswahl;
            //Funktionstaste abfragen
            if (event.getKeyCode() == KeyEvent.VK_ESCAPE)
            {
            auswahl = JOptionPane.showConfirmDialog(cnt, "Möchten sie das Spiel beenden ? ", "Spiel beenden ?", 0);
            switch(auswahl)
            {
                case 0: System.exit(0);
                case 1: break;
            }
         }

      }

    public void keyTyped(KeyEvent event){};
    public void keyPressed(KeyEvent event){};
    });

    }

}
